<template>
  <div class="orderlist">
    <div>
      <el-tabs v-model="search.status" @tab-click="tabClick">
        <el-tab-pane
          :label="item.name + '(' + item.num + ')'"
          :name="item.name == '全部' ? '' : item.name"
          v-for="item in orderStatic"
          :key="item.id"
        ></el-tab-pane>
      </el-tabs>
    </div>
    <div class="form">
      <el-form
        :model="search"
        ref="searchForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="配送方式" prop="pickupInstore">
          <el-radio-group v-model="search.pickupInstore">
            <el-radio label="全部"></el-radio>
            <el-radio label="自提"></el-radio>
            <el-radio label="配送"></el-radio>
          </el-radio-group>
        </el-form-item>
        <div class="sb">
          <div class="left">
            <el-form-item label="订单编号" prop="OrderNum">
              <el-input
                v-model="search.OrderNum"
                placeholder="订单编号"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品编号" prop="ParentOrderID">
              <el-input
                v-model="search.ParentOrderID"
                placeholder="商品编号"
              ></el-input>
            </el-form-item>
            <el-form-item label="所属门店" prop="shoppingAdd">
              <el-input
                v-model="search.shoppingAdd"
                placeholder="所属门店"
              ></el-input>
            </el-form-item>
            <div style="display: flex">
              <el-form-item label="会员账号" prop="customerId">
                <el-input
                  v-model="search.customerId"
                  placeholder="会员账号"
                ></el-input>
              </el-form-item>
              <el-form-item label="收货人" prop="consignee">
                <el-input
                  v-model="search.consignee"
                  placeholder="收货人姓名/手机号码"
                ></el-input>
              </el-form-item>

              <el-form-item label="下单时间">
                <el-col :span="11">
                  <el-form-item prop="createStart">
                    <el-date-picker
                      type="date"
                      placeholder="选择开始时间"
                      v-model="search.createStart"
                      style="width: 100%"
                    ></el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col class="line" :span="2">-</el-col>
                <el-col :span="11">
                  <el-form-item prop="createEnd">
                    <el-time-picker
                      placeholder="选择结束时间"
                      v-model="search.createEnd"
                      style="width: 100%"
                    ></el-time-picker>
                  </el-form-item>
                </el-col>
              </el-form-item>
            </div>
          </div>
          <div class="right">
            <el-link type="primary" @click="resetForm('searchForm')"
              >重置筛选条件</el-link
            >
            <el-button
              type="primary"
              @click=" submitForm('searchForm')"
              icon="el-icon-search"
              >查询</el-button
            >
          </div>
        </div>
        <!-- 导出 -->
        <el-button @click="text()">
            <!-- 测试 -->
            <download-excel
              :data="orderList"
              :fields="json_fields"
              worksheet="My Worksheet"
              name="订单列表.xls"
            >
              导出
            </download-excel>
          </el-button>
      </el-form>
    </div>
    <div>
      <el-table
        ref="multipleTable"
        :data="orderList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection"> </el-table-column>
        <el-table-column prop="OrderNum" label="订单编号" width="120">
        </el-table-column>
        <el-table-column label="下单日期">
          <template slot-scope="scope">{{ scope.row.createAt }}</template>
        </el-table-column>
        <el-table-column prop="customerId" label="会员账号"> </el-table-column>
        <el-table-column
          prop="customer_total_amount"
          label="订单总额"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="shoppingAdd" label="所属门店"></el-table-column>
        <el-table-column label="订单状态">
          <template slot-scope="scope">
            <span style="color: limegreen" v-if="scope.row.status == '已完成'"
              >已完成</span
            >
            <span style="color: red" v-else-if="scope.row.status == '待付款'"
              >待付款</span
            >
            <span style="color: purple" v-else-if="scope.row.status == '待发货'"
              >待发货</span
            >
            <span style="color: orange" v-else-if="scope.row.status == '待退款'"
              >待退款</span
            >
            <span v-else>{{ scope.row.status }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              @click="$router.push('/admin/orderdetail/'+scope.row.OrderNum)"
              type="text"
              icon="el-icon-search"
              >查看订单</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="pagination.current"
          :page-size="pagination.size"
          :page-sizes="[5,10,20]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { GetOrderStatic, GetOrderList } from "@/api/order1";
export default {
  data() {
    return {
      orderStatic: [], //静态统计数据
      orderList: [], //订单列表
      pagination: {}, //分页数据
      search: {  }, //查询条件
      activeName: "second",
      excel:[],
      json_fields: {              //表头设计
        "订单编号":"OrderNum",
          "下单日期": "createAt",
          "会员账号": "customerId",
          "订单总额": "refunded_amount",
          "所属门店": "shoppingAdd",
          "订单状态": "status",
        },
    };
  },
  created() {
    //获取订单统计数据
    GetOrderStatic().then((res) => {
      this.orderStatic = res.data.list;
    });
    this.getOrderList();
  },
  methods: {
    text() {
      console.log(1,"测试导出按钮");
      console.log(this.excel);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.getOrderList();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 处理size变化
    handleSizeChange(val) {
      // 当每页多少条发生变化时候，重新更新搜索条件size
      // 重新获取员工列表
      console.log(`每页 ${val} 条`);
      this.search.size = val;
      this.getOrderList();
    },
    // 处理current变化
    handleCurrentChange(val) {
      // 当当前页发生变化时候更新 搜索条件的当前页
      // 获取数据
      this.search.current = val;
      this.getOrderList();
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.getOrderList();
    },
    //获取订单列表（这个方法会多次执行）
    getOrderList() {
      // var search = {};
      // // 如果是空的字段，不添加到搜索条件
      // for (var k in this.search) {
      //   if (this.search[k]) {
      //     search[k] = this.search[k];
      //   }
      // }
      if (this.search.status == 0) {
        this.search.status = "";
      }
      // 如果面试官问，你在使用这个组件遇到哪些问题时，你就说当时有个tabs组件，默认的要是你给他绑定的为空或者null时哦，他就默认返回0，所以我直接修改数据，让它是0是让它为空
      GetOrderList(this.search)
        .then((res) => {
          this.orderList = res.data.data;
          this.pagination = res.data.pagnation;
          this.pagination.current = this.pagnation.current * 1;
          this.pagination.size = this.pagnation.size * 1;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    tabClick(tab) {
      //   console.log(tab, event);
      this.search.status = tab.name;
      //   console.log(tab);
      this.getOrderList();
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style lang="scss" scoped>
.orderlist {
  height: 100%;
  overflow-y: auto;
}
.sb {
  display: flex;
  justify-content: space-between;
  .left {
    width: 80%;
    display: flex;
    flex-wrap: wrap;
  }
}
</style>
